package com.fau.wmbj.server.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.fau.wmbj.server.config.CurrentUser;
import com.fau.wmbj.server.entity.User;
import com.fau.wmbj.server.flowable.domain.bo.*;
import com.fau.wmbj.server.flowable.service.ActTaskService;
import com.fau.wmbj.server.idempotent.annotation.RepeatSubmit;
import com.fau.wmbj.server.utils.Message;
import com.fau.wmbj.server.utils.Pageable;
import com.fau.wmbj.server.utils.QueryUtils;
import org.flowable.engine.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 任务管理
 */
@RestController
@RequestMapping("/workflow/task")
public class ActTaskController {

    @Resource
    private ActTaskService actTaskService;
    @Resource
    private TaskService taskService;


    /**
     * 办理任务
     *
     * @param completeTaskBo 办理任务参数
     */
    @RepeatSubmit()
    @PostMapping("/completeTask")
    public Message completeTask(@RequestBody CompleteTaskBo completeTaskBo) {
        return Message.success(actTaskService.completeTask(completeTaskBo));
    }

    /**
     * 查询当前用户的待办任务
     *
     * @param pageable 参数
     */
    @PostMapping("/getPageByTaskWait")
    @ResponseBody
    public Message getPageByTaskWait(@RequestBody Pageable pageable, @CurrentUser User user) {
        return Message.success(actTaskService.getPageByTaskWait(pageable, user));
    }

    /**
     * 查询当前租户所有待办任务
     *
     * @param taskBo 参数
     */
    @GetMapping("/getPageByAllTaskWait")
    public Message getPageByAllTaskWait(TaskBo taskBo, Pageable pageable) {
        return Message.success(actTaskService.getPageByAllTaskWait(taskBo, pageable));
    }

    /**
     * 查询当前用户的已办任务
     *
     * @param taskBo 参数
     */
    @GetMapping("/getPageByTaskFinish")
    public Message getPageByTaskFinish(TaskBo taskBo, Pageable pageable) {
        return Message.success(actTaskService.getPageByTaskFinish(taskBo, pageable));
    }

    /**
     * 查询当前用户的抄送
     *
     * @param taskBo 参数
     */
    @GetMapping("/getPageByTaskCopy")
    public Message getPageByTaskCopy(TaskBo taskBo, Pageable pageable) {
        return Message.success(actTaskService.getPageByTaskCopy(taskBo, pageable));
    }

    /**
     * 查询当前租户所有已办任务
     *
     * @param taskBo 参数
     */
    @GetMapping("/getPageByAllTaskFinish")
    public Message getPageByAllTaskFinish(TaskBo taskBo, Pageable pageable) {
        return Message.success(actTaskService.getPageByAllTaskFinish(taskBo, pageable));
    }

    /**
     * 签收（拾取）任务
     *
     * @param taskId 任务id
     */
    @RepeatSubmit()
    @PostMapping("/claim/{taskId}")
    public Message claimTask(@PathVariable String taskId) {
        try {

            User user = (User) StpUtil.getSession().get("user");
            taskService.claim(taskId, Convert.toStr(user.getId()));
            return Message.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error("签收任务失败：" + e.getMessage());
        }
    }

    /**
     * 归还（拾取的）任务
     *
     * @param taskId 任务id
     */
    @RepeatSubmit()
    @PostMapping("/returnTask/{taskId}")
    public Message returnTask(@PathVariable String taskId) {
        try {
            taskService.setAssignee(taskId, null);
            return Message.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error("归还任务失败：" + e.getMessage());
        }
    }

    /**
     * 委派任务
     *
     * @param delegateBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/delegateTask")
    public Message delegateTask(@RequestBody DelegateBo delegateBo) {
        return Message.success(actTaskService.delegateTask(delegateBo));
    }

    /**
     * 终止任务
     *
     * @param terminationBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/terminationTask")
    public Message terminationTask(@RequestBody TerminationBo terminationBo) {
        return Message.success(actTaskService.terminationTask(terminationBo));
    }

    /**
     * 转办任务
     *
     * @param transmitBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/transferTask")
    public Message transferTask(@RequestBody TransmitBo transmitBo) {
        return Message.success(actTaskService.transferTask(transmitBo));
    }

    /**
     * 会签任务加签
     *
     * @param addMultiBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/addMultiInstanceExecution")
    public Message addMultiInstanceExecution(@RequestBody AddMultiBo addMultiBo) {
        return Message.success(actTaskService.addMultiInstanceExecution(addMultiBo));
    }

    /**
     * 会签任务减签
     *
     * @param deleteMultiBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/deleteMultiInstanceExecution")
    public Message deleteMultiInstanceExecution(@RequestBody DeleteMultiBo deleteMultiBo) {
        return Message.success(actTaskService.deleteMultiInstanceExecution(deleteMultiBo));
    }

    /**
     * 驳回审批
     *
     * @param backProcessBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/backProcess")
    public Message backProcess(@RequestBody BackProcessBo backProcessBo) {
        return Message.success(actTaskService.backProcess(backProcessBo));
    }

    /**
     * 获取当前任务
     *
     * @param taskId 任务id
     */
    @GetMapping("/getTaskById/{taskId}")
    public Message getTaskById(@PathVariable String taskId) {
        return Message.success(QueryUtils.getTask(taskId));
    }


    /**
     * 修改任务办理人
     *
     * @param taskIds 任务id
     * @param userId  办理人id
     */
    @RepeatSubmit()
    @PutMapping("/updateAssignee/{taskIds}/{userId}")
    public Message updateAssignee(@PathVariable String[] taskIds, @PathVariable String userId) {
        return Message.success(actTaskService.updateAssignee(taskIds, userId));
    }

    /**
     * 查询流程变量
     *
     * @param taskId 任务id
     */
    @GetMapping("/getInstanceVariable/{taskId}")
    public Message getProcessInstVariable(@PathVariable String taskId) {
        return Message.success(actTaskService.getInstanceVariable(taskId));
    }

    /**
     * 查询工作流任务用户选择加签人员
     *
     * @param taskId 任务id
     */
    @GetMapping("/getTaskUserIdsByAddMultiInstance/{taskId}")
    public Message getTaskUserIdsByAddMultiInstance(@PathVariable String taskId) {
        return Message.success(actTaskService.getTaskUserIdsByAddMultiInstance(taskId));
    }

    /**
     * 查询工作流选择减签人员
     *
     * @param taskId 任务id
     */
    @GetMapping("/getListByDeleteMultiInstance/{taskId}")
    public Message getListByDeleteMultiInstance(@PathVariable String taskId) {
        return Message.success(actTaskService.getListByDeleteMultiInstance(taskId));
    }

}
